package com.chhuang.ewcrm.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author chhuang
 * @create 2020-10-13-15:01
 */
public class StringUtil {
    public static boolean isPositiveInteger(String str) {
        return str.matches("^[0-9]+$");
    }

    public static boolean isInteger(String str) {
        return str.matches("^[-+]?[0-9]+$");
    }

    public static boolean isPositiveNumber(String str) {
        return str.matches("^[0-9]+\\.?[0-9]*$");
    }

    public static boolean isNumber(String str) {
        return str.matches("^[-+]?[0-9]+\\.?[0-9]*$");
    }

    public static boolean isIP(String str) {
        return str.matches("^((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))$");
    }


    /**
     * 下划线字符串转驼峰字符串
     * @param str
     * @return
     */
    public static String lineToHump(String str) {
        Pattern linePattern = Pattern.compile("_(\\w)");
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰字符串转下划线字符串
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Pattern humpPattern = Pattern.compile("[A-Z]");
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
